package com.returnsoft.softphone.eao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import com.returnsoft.softphone.dto.AgentDto;
import com.returnsoft.softphone.dto.SupervisorDto;
import com.returnsoft.softphone.entity.Supervisor;
import com.returnsoft.softphone.exception.CCenterExcepcion;
import com.returnsoft.softphone.exception.EaoExcepcion;

@LocalBean
@Stateless
public class SupervisorEao implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@PersistenceContext(name = "CCenterEJB", unitName = "CCenterEJB")
	private EntityManager em;

	public Supervisor findSupervisor(Integer userId) throws EaoExcepcion {

		try {
			Query q = em.createQuery("SELECT s.id " + "FROM Supervisor s "
					+ "WHERE s.userId = :userId");

			q.setParameter("userId", userId);

			Integer supervisorId = (Integer) q.getSingleResult();

			Supervisor agent = new Supervisor();
			agent.setId(supervisorId);

			return agent;

		} catch (NoResultException e) {
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}

	}

	@SuppressWarnings("unchecked")
	public List<SupervisorDto> findByCampaigns(List<Integer> campaignsId)
			throws EaoExcepcion {
		try {

			String query =

			"select s.id,p.firstname,p.lastname from ccenter.supervisor s "
					+ "left join ccenter.supervisor_campaign sc on sc.supervisor_id=s.id "
					+ "left join security.user u on u.id=s.user_id "
					+ "left join security.person p on p.id=u.person_id "
					+ "where s.id>0 ";

			if (campaignsId != null && campaignsId.size() > 0) {
				query += " and sc.campaign_id in ";
				query += " ( ";
				for (int i = 0; i < campaignsId.size(); i++) {
					Integer campaignId = campaignsId.get(i);
					if (i < campaignsId.size() - 1) {
						query += campaignId + ",";
					} else {
						query += campaignId;
					}
				}
				query += " ) ";
			}

			query += " group by s.id ";

			Query q = em.createNativeQuery(query);

			List<Object[]> supervisorObjectList = (List<Object[]>) q
					.getResultList();

			List<SupervisorDto> supervisors = new ArrayList<SupervisorDto>();
			if (supervisorObjectList.size() > 0) {
				for (Object[] userObject : supervisorObjectList) {

					Integer supervisorId = (Integer) userObject[0];
					String supervisorFirstname = (String) userObject[1];
					String supervisorLastname = (String) userObject[2];

					SupervisorDto supervisor = new SupervisorDto();
					supervisor.setId(supervisorId);
					supervisor.setFirstname(supervisorFirstname);
					supervisor.setLastname(supervisorLastname);

					supervisors.add(supervisor);
				}
			}

			return supervisors;

		} catch (Exception e) {
			e.printStackTrace();
			throw new EaoExcepcion(e.getMessage());
		}
	}

}
